﻿#include <QDateTimeEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QListWidget>
#include <QListWidgetItem>

#include "PropDelegate.h"
#include "MenuInfo.h"
#include "CheckComboBox.h"
#include <QtGlobal>
#include <QDateTime>
#include <QLineEdit>
#include <QColor>
#include <QColorDialog>
#include "relatedlg.h"
#include <QIntValidator>
#include <QDoubleValidator>
#include <QRegExp>
#include <QRegExpValidator>
//#include "ChoiceIndexNoDlg.h"
#include <QChar>

PropDelegate::PropDelegate( MyTableData *pTableData, QObject *parent): QItemDelegate(parent)
{
	m_pTableData = NULL;
	m_pMenuInfo = NULL;
	m_pTableData = pTableData;
	m_pTableData->GetMenuInfoPoint(m_pMenuInfo);
}

PropDelegate::~PropDelegate(void)
{

}

// 当双击表格里的某一个元素时，会调用这个函数，创造一个Editor
QWidget *PropDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	if (!m_pTableData)
	{
		return QItemDelegate::createEditor(parent, option, index);
	}

	if (!m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::createEditor(parent, option, index);
	}
	int		FieldNo = index.row();
	int		RecordNo = 0;
	short	DisplayType = 0;
	int		ColumnId = 0;
	if (!m_pTableData->IsRelateColumnEnableEdit(RecordNo, FieldNo))
	{
		return NULL;
	}
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	ColumnId = m_pTableData->GetFieldColumnId(FieldNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
		{
			QComboBox *editor = new QComboBox(parent);
			int		Index = 0;
			QString ItemText ;
			ItemText = QString::fromLocal8Bit("否");
			int		UserData = SYS_STATE_INVALID;
			editor->insertItem(Index++, ItemText, QVariant(UserData));
			ItemText = QString::fromLocal8Bit("是");
			UserData = SYS_STATE_VALID;
			editor->insertItem(Index, ItemText, QVariant(UserData));

			int i = editor->findData(index.model()->data(index, Qt::UserRole).toInt());
			editor->setCurrentIndex(i);
			return editor;
		}
		break;
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *MenuEditor = new QComboBox(parent);
			QString ItemText;
			int		UserData = 0;
			int	UserDataBefore = 0;
			int		MenuNum = 0;
			ItemMenu *pMenuInfo = NULL;
			Field_Menu FieldMenuInfoTemp;
			cColumn_Info column_info_temp;
			m_pTableData->GetFieldColumnInfo(FieldNo, column_info_temp);
			if (m_pMenuInfo)
			{
				if (QString::fromLocal8Bit(column_info_temp.ColumnEngName)=="m_ProtType")
				{
					UserDataBefore=index.model()->data(index.model()->index(FieldNo-1,1),Qt::UserRole).toInt();
					QString MenuName=m_pMenuInfo->GetMenuNameByTaskType(UserDataBefore);
					m_pMenuInfo->GetMenuInfo(FieldMenuInfoTemp,MenuName.toLocal8Bit().data());
					pMenuInfo=FieldMenuInfoTemp.pMenu;
					MenuNum=FieldMenuInfoTemp.menuNum;
				}
				else
				{
					MenuNum = m_pMenuInfo->GetMenu(ColumnId, pMenuInfo);
				}
				if (MenuNum>0 && pMenuInfo)
				{
					for (int i=0; i<MenuNum; i++)
					{
						if (pMenuInfo[i].pDisplayText)
						{
							ItemText = QString::fromLocal8Bit(pMenuInfo[i].pDisplayText);
						}
						UserData = pMenuInfo[i].ActVal;
						MenuEditor->insertItem(i, ItemText, QVariant(UserData));
					}
					int CurIndex = MenuEditor->findData(index.model()->data(index, Qt::UserRole).toInt());
					if (CurIndex>= 0 && CurIndex<MenuNum)
					{
						MenuEditor->setCurrentIndex(CurIndex);
					}else
					{
						MenuEditor->setCurrentIndex(0);
					}
					return MenuEditor;
				}
			}

			return QItemDelegate::createEditor(parent, option, index);
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = new CheckComboBox(parent);
			QString			ItemText;
			int				UserData = 0;
			int				MenuNum = 0;
			ItemMenu		*pMenuInfo = NULL;
			int				ItemData = index.model()->data(index, Qt::UserRole).toInt();
			if (m_pMenuInfo)
			{
				MenuNum = m_pMenuInfo->GetMenu(ColumnId, pMenuInfo);
				if (MenuNum>0 && pMenuInfo)
				{
					MultiMenuEditor->setMaxCount(MenuNum);
					for (int i=0; i<MenuNum; i++)
					{
						if (pMenuInfo[i].pDisplayText)
						{
							ItemText = QString::fromLocal8Bit(pMenuInfo[i].pDisplayText);
						}
						UserData = pMenuInfo[i].ActVal;
						MultiMenuEditor->addItemText(ItemText, QVariant(UserData), false);
						if ((UserData&ItemData)>0)
						{
							MultiMenuEditor->setCheck(i, true);
						}else
						{
							MultiMenuEditor->setCheck(i, false);
						}
					}
					return MultiMenuEditor;
				}
			}

			return QItemDelegate::createEditor(parent, option, index);
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *editor = new QTimeEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("hh:mm:ss");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			QDateEdit *editor = new QDateEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("yyyy-M-dd");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_DATETIME:
		{
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setInputMask("00.00");
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *editor = new QDateTimeEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("yyyy-M-ddThh:mm:ss");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
// 			if (m_pTableData->IsNewDataUnSaved())
// 			{
// 				QMessageBox::information(NULL, QStringEx::fromLocal8Bit("数据库组态"), QStringEx::fromLocal8Bit("有未保存的修改内容，请先保存再进行关联组态，防止修改内容丢失！"));
// 				return NULL;
// 			}


			unsigned int	OldItemData = index.model()->data(index, Qt::UserRole).toUInt();
			unsigned int	CurRecordId = 0;
			int		        CurSeltNo =	m_pTableData->GetCurSelectRecordNo();
			m_pTableData->GetCurRecordIdbyRowIndex(CurSeltNo, CurRecordId);
			cColumn_Info ColumnInfo;
			if (m_pTableData->GetFieldColumnInfo(FieldNo, ColumnInfo)>0)
			{
				RelateDlg *dlgEditor = new RelateDlg(&ColumnInfo, OldItemData, CurRecordId/*, parent*/);
				return dlgEditor;
			}
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = new QColorDialog();
			return pColorDlg;
		}	
		break;
// 	case DISPLAY_TYPE_TABLE:
// 		{
// 
// 		}
// 		break;
// 	case DISPLAY_TYPE_COLUMN:
// 		{
// 
// 		}
// 		break;
	case DISPLAY_TYPE_CHAR:
		{
			QValidator *validator = new QIntValidator(-0x80, 0x7F, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_UCHAR:
		{
			QValidator *validator = new QIntValidator(0x0, 0xFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_SHORT:
		{
			QValidator *validator = new QIntValidator(-0x8000, 0x7FFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_USHORT:
		{
			QValidator *validator = new QIntValidator(0x0, 0xFFFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_INT32:
		{
			QValidator *validator = new QIntValidator(-0x80000000, 0x7FFFFFFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_UINT32:
		{
			QRegExp rx("\\d{0,10}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_LONGLONG:
		{
			QRegExp rx("[-0-9]\\d{0,20}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_ULONGLONG:
		{
			QRegExp rx("\\d{0,20}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_FLOAT:
	case DISPLAY_TYPE_DOUBLE:
		{
			QValidator *validator = new QDoubleValidator(parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;

	case DISPLAY_TYPE_HEX:
		{
			QRegExp rx("[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;

	case DISPLAY_TYPE_EXP:
	case DISPLAY_TYPE_INDEXNO:
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_PASSWORD:
	default:
		{
			return QItemDelegate::createEditor(parent, option, index);
		}
		break;
	}
	return QItemDelegate::createEditor(parent, option, index);
}

void PropDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const
{
	//qDebug() << "setEditorData ItemData" << QDateTime::currentDateTime();
	if (!m_pTableData)
	{
		return QItemDelegate::setEditorData(editor, index);
	}

	if (!m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::setEditorData(editor, index);
	}
	int		FieldNo = index.row();
	short	DisplayType = 0;
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
			if(comboBox)
			{
				int i = comboBox->findData(index.model()->data(index, Qt::UserRole).toInt());
				comboBox->setCurrentIndex(i);
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = qobject_cast<CheckComboBox*>(editor);
			if (MultiMenuEditor)
			{
				int ItemData = index.model()->data(index, Qt::UserRole).toInt();
				int DataBitNum = 8*sizeof(int);
				int ItemNum = MultiMenuEditor->maxCount();
				int bitVal = 0;
				DataBitNum = qMin(DataBitNum, ItemNum);
				for (int i=0; i<DataBitNum ; i++)
				{
					bitVal = MultiMenuEditor->itemData(i).toInt();
					if ((bitVal&ItemData)>0)
					{
						MultiMenuEditor->setCheck(i, true);
					}else
					{
						MultiMenuEditor->setCheck(i, false);
					}
				}
			}
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *dateEditor = qobject_cast<QTimeEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int timeVlue = index.model()->data(index, Qt::UserRole).toUInt();
				QTime qTime;
				qTime.setHMS(timeVlue%1000000/10000, timeVlue%10000/100,timeVlue%100);
				dateEditor->setTime(qTime);
			}
		}
		break;
	case DISPLAY_TYPE_DATE:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
			if (dateEditor) 
			{
				QDateTime dateTime ;
				dateTime.setTime_t(index.model()->data(index, Qt::UserRole).toUInt());
				dateEditor->setDateTime(dateTime);
				//dateEditor->setDateTime(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "yyyy/M/d"));
			}
		}
		break;
	case DISPLAY_TYPE_DATETIME:
		{
			QLineEdit *dateEditor = qobject_cast<QLineEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int DateValue=index.model()->data(index, Qt::UserRole).toUInt();
				dateEditor->setText(QString::fromLocal8Bit("%1.%2").arg(DateValue%10000/100).arg(DateValue%100));
			}
		}
		break;
// 	case DISPLAY_TYPE_RELATED:
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = NULL;
			pColorDlg = qobject_cast<QColorDialog *>(editor);
			if (pColorDlg) 
			{
				pColorDlg->setCurrentColor(QColor(QRgb(index.model()->data(index, Qt::UserRole).toUInt())));
			}
		}
		break;
	case DISPLAY_TYPE_CHAR:
	case DISPLAY_TYPE_UCHAR:
	case DISPLAY_TYPE_SHORT:
	case DISPLAY_TYPE_USHORT:
	case DISPLAY_TYPE_INT32:
	case DISPLAY_TYPE_UINT32:
	case DISPLAY_TYPE_LONGLONG:
	case DISPLAY_TYPE_ULONGLONG:
	case DISPLAY_TYPE_FLOAT:
	case DISPLAY_TYPE_DOUBLE:
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_HEX:
	case DISPLAY_TYPE_PASSWORD:
	//case DISPLAY_TYPE_BINARY:
	default:
		{
			return QItemDelegate::setEditorData(editor, index);
		}
		break;
	}
}

void PropDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
{
	if (!index.isValid() || !model || !editor)
	{
		return;
	}
	if (!m_pTableData || !m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::setModelData(editor, model, index);
	}
	
	int			FieldNo = index.row();
	int			RecordNo = 0;
	int			ColumnId = 0;
	short		DisplayType = 0;

	bool		bRefresh = false;
	QVariant	OldUserData = model->data(index, Qt::UserRole);
	QVariant	NewUserData;
	
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	ColumnId = m_pTableData->GetFieldColumnId(FieldNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
			if(comboBox)
			{
				int ItemData = comboBox->itemData(comboBox->currentIndex()).toInt();				
				model->setData(index, comboBox->currentText(), Qt::DisplayRole);
				model->setData(index, ItemData, Qt::UserRole);
				
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = qobject_cast<CheckComboBox*>(editor);
			if (MultiMenuEditor)
			{
				int DataBitNum = 8*sizeof(int);
				int ItemNum = MultiMenuEditor->maxCount();
				int ItemData = 0;
				DataBitNum = qMin(DataBitNum, ItemNum);
				for (int i=0; i<DataBitNum ; i++)
				{
					if (MultiMenuEditor->isChecked(i))
					{	
						ItemData += MultiMenuEditor->itemData(i).toInt();
					}
				}

				model->setData(index, MultiMenuEditor->getSelString(), Qt::DisplayRole);
				model->setData(index, ItemData, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *dateEditor = qobject_cast<QTimeEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int timevalue = dateEditor->time().hour()*10000+dateEditor->time().minute()*100+dateEditor->time().second();

				model->setData(index, dateEditor->time().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, timevalue, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			QDateEdit *dateEditor = qobject_cast<QDateEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int	timetVal = dateEditor->dateTime().toTime_t();
				model->setData(index, dateEditor->date().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, timetVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DATETIME:
		{
			QLineEdit *dateEditor = qobject_cast<QLineEdit *>(editor);
			if (dateEditor) 
			{
				QString		str;
				unsigned int DateVal=0;
				str = dateEditor->text();
				str = str.trimmed();
				QStringList Strlst = str.split(QLatin1String("."));
				if (Strlst.count() >= 2)
				{
					DateVal = Strlst.at(0).toUInt()%100*100+Strlst.at(1).toUInt()%100;
				}
				model->setData(index, str, Qt::DisplayRole);
				model->setData(index, DateVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int	timetVal = dateEditor->dateTime().toTime_t();
				model->setData(index, dateEditor->dateTime().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, timetVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			QString ItemText;
			unsigned int	NewItemData = 0;
			RelateDlg *dlgEditor = qobject_cast< RelateDlg *>(editor);
			if (dlgEditor) 
			{
				if (dlgEditor->GetRelateData(NewItemData, ItemText)>0)
				{
					//dlgEditor->GetRecordId(RecId1,RecId2,RecId3);
					model->setData(index, ItemText, Qt::DisplayRole);
					model->setData(index, NewItemData, Qt::UserRole);
					//bRefresh = true;
				}
			}
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = NULL;
			unsigned int	NewItemData = 0;
			QString ItemText;
			QColor curColor;
			pColorDlg = qobject_cast<QColorDialog *>(editor);
			if (pColorDlg) 
			{
				curColor = pColorDlg->currentColor();
				NewItemData = curColor.rgba();
				ItemText = QString("(%1, %2, %3 ,%4)").arg(curColor.red()).arg(curColor.green()).arg(curColor.blue()).arg(curColor.alpha());
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, curColor, Qt::DecorationRole);
				//model->setData(index, curColor, Qt::BackgroundRole);
				model->setData(index, NewItemData, Qt::UserRole);
			}
		}		
		break;
		// 	case DISPLAY_TYPE_TABLE:
		// 		{
		// 
		// 		}
		// 		break;
		// 	case DISPLAY_TYPE_COLUMN:
		// 		{
		// 
		// 		}
		// 		break;
	case DISPLAY_TYPE_CHAR:
		{
			QString ItemText;
			char	CharVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				CharVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, CharVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_UCHAR:
		{
			QString			ItemText;
			unsigned char	uCharVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uCharVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uCharVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_SHORT:
		{
			QString ItemText;
			short	ShortVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				ShortVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, ShortVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_USHORT:
		{
			QString			ItemText;
			unsigned short	uShortVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uShortVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uShortVal, Qt::UserRole);
			}
		}
		break;;
	case DISPLAY_TYPE_INT32:
		{
			QString ItemText;
			int		IntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				IntVal = ItemText.toInt();
// 				XINT RelatedFieldNo=0;
// 				if (m_pTableData->GetRelatedFieldNo(FieldNo, RelatedFieldNo)>0)
// 				{
// 					QString CodeStr;
// 					if (m_pTableData->GetNewCodeStr(rowNo, IntVal, CodeStr)>0)
// 					{
// 						SetRelateColumnData(model, index, rowNo, RelatedFieldNo, CodeStr, QVariant(CodeStr));
// 					}
// 				}
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, IntVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_UINT32:
		{
			QString			ItemText;
			unsigned int	uIntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uIntVal = ItemText.toUInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uIntVal, Qt::UserRole);
			}
		}
		break;

	
	case DISPLAY_TYPE_FLOAT:
		{
			QString		ItemText;
			float		floatVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				floatVal = ItemText.toFloat();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, floatVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DOUBLE:
		{
			QString		ItemText;
			double		doubleVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				doubleVal = ItemText.toDouble();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, doubleVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_PASSWORD:
		{
			QString			ItemText;
			int				ColumnNo = 0;
			unsigned short	ColDataLen = 0;
			ColumnNo = m_pTableData->GetFieldColumnNo(FieldNo);
			ColDataLen = m_pTableData->GetCurColumnLength(ColumnNo);
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				ItemText = ItemText.trimmed();    //jwj 2013-08-19

				//jwj 2012-10-24
				QByteArray Text = ItemText.toLocal8Bit();
				if (Text.length()>ColDataLen)
				{
					QMessageBox::question(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("输入内容超出字段长度！"));
					model->setData(index, Qt::red, Qt::TextColorRole);
					return ;
				}
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, ItemText, Qt::UserRole);

			}
		}
		break;
	
	case DISPLAY_TYPE_HEX:		//jwj 2011-10-31
		{
			QString			ItemText;
			unsigned int	uIntVal = 0;
			QChar			NumbChar;
			unsigned char	NumbInt=0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text().toUpper();
				ItemText = ItemText.trimmed();       //jwj 2013-08-19
				if (ItemText.length()>0)
				{
					for (int i=0; i<4 && i<ItemText.length(); i++)
					{
						NumbChar = ItemText.at(i);
						if (NumbChar.isNumber())
						{
							NumbInt = NumbChar.toAscii()-0x30;
						}else
						{
							if (NumbChar.toAscii()>=65 && NumbChar.toAscii()<=70)
							{
								NumbInt = NumbChar.toAscii()-55;
							}
							else if (NumbChar.toAscii()>=97 && NumbChar.toAscii()<= 102)
							{
								NumbInt = NumbChar.toAscii()-87;
							}
							else
							{
								uIntVal = uIntVal*0x10;
								continue;
							}
						}
						uIntVal = uIntVal*0x10+NumbInt;
					}
		
					model->setData(index, ItemText, Qt::DisplayRole);
					model->setData(index, uIntVal, Qt::UserRole);
				}
			}
		}
		break;
	case DISPLAY_TYPE_IP_CFG:
		{
			QString			ItemText;
			unsigned int	uIntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				QStringList Tmplist = ItemText.split(".");
				QString str;
				quint8 Val=0;
				uIntVal = 0;
				for (int i=0; i<Tmplist.size(); i++)
				{
					Val = Tmplist.at(i).toUInt();
					uIntVal *= 0x100;
					uIntVal += Val;
				}
				//uIntVal = ItemText.toUInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uIntVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_EXP:
	case DISPLAY_TYPE_INDEXNO:

	default:
		{
			QItemDelegate::setModelData(editor, model, index);
		}
		break;
	}
	
	NewUserData = model->data(index, Qt::UserRole);
	if (NewUserData != OldUserData && !bRefresh)
	{
		RecordNo = m_pTableData->GetCurSelectRecordNo();
		
		m_pTableData->UpDateDataChang(RecordNo, FieldNo,NewUserData);
		//model->setData(index, Qt::red, Qt::TextColorRole);
		emit Signal_UserDataChang(FieldNo, false);
	}
	if (bRefresh)
	{
		emit Signal_UserDataChang(FieldNo,true);
	}
	return ;
}
//设置相关记录同一字段内容
void PropDelegate::SetSibRecordColumn(QAbstractItemModel *model, const QModelIndex &OldIndex, int SibRowNo, int FieldNo, QString ItemText, QVariant ItemData) const
{
	if (!model || !OldIndex.isValid() || SibRowNo <0 || FieldNo<0)
	{
		return ;
	}

	QModelIndex SibIndex;
	SibIndex = OldIndex.sibling(SibRowNo, FieldNo);
	if (SibIndex.isValid())
	{
		model->setData(SibIndex, ItemText, Qt::DisplayRole);
		model->setData(SibIndex, ItemData, Qt::UserRole);
		model->setData(SibIndex, Qt::red, Qt::TextColorRole);
		m_pTableData->UpDateDataChang(SibRowNo, FieldNo, ItemData);
	}
}

void PropDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & index )const
{
	if (!editor || !index.isValid())
	{
		return;
	}
	//int		FieldNo = index.column();
	int		FieldNo = index.row();
	short	DisplayType = 0;
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	if (DISPLAY_TYPE_RELATED != DisplayType &&
/*		DISPLAY_TYPE_NODE_CFG_STATE != DisplayType &&*/
		DISPLAY_TYPE_INDEXNO != DisplayType &&
		DISPLAY_TYPE_EXP != DisplayType )	//jwj 2012-11-02
	{
		editor->setGeometry(option.rect);
	}
}
